﻿package  webCode {
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.DisplayObject;
	import flash.events.*;
	import flash.display.Stage;
	import flash.display.DisplayObject;
	import flash.events.*;
	
<<<<<<< .mine
	public class Web extends Sprite{
=======
	public class Web extends Shape{
>>>>>>> .r5

		//Attributes
		var nodes:Array;
		var color:uint;
		var thickness:Number;
<<<<<<< .mine
		var sides:Number;
		var center:webNode;
		var theta:Number;
		var rads:Number;
		var radius:Number;
		var rings:Number; //number of rings of nodes around the center point
		var springs:Array;
		var v:Number;
		var centerDrawn:Boolean;
=======
		var sides:Number;
		var center:Vertex;
		var theta:Number;
		var rads:Number;
		var radius:Number;
		var rings:Number; //number of rings of nodes around the center point
		var springs:Array;
		var v:Number;
>>>>>>> .r5
		
<<<<<<< .mine
		public function Web(a:Number, b:Number, radius:Number, thickness:Number =1, color:uint=0x40FF00) {
=======
		public function Web(center:Vertex, radius:Number, thickness:Number =1, color:uint=0x40FF00) {
>>>>>>> .r5
			this.thickness=thickness;
			nodes = new Array();
			springs = new Array();
			this.color = color;
<<<<<<< .mine
			//this.center = center;
			this.radius = radius;
			centerDrawn = false;
			sides = 8;
			rings=5;
			init(a, b);
			
=======
			this.center = center;
			this.radius = radius;
			sides = 8;
			rings=5;
			v=0;
			init();
>>>>>>> .r5
			
		}
		
		public function init(i:Number, j:Number):void
		{
<<<<<<< .mine
			center = new webNode();
			center.x=i;
			center.y=j;
			nodes.push(center);
			addChild(center);
			v=1;
			makeNodes(rings);
			setStaticNodes();
			setDrags();
			addVSprings();
			drawSprings();
=======
			makeNodes(rings);
			setStaticNodes();
			setDrags();
			addVSprings();
			drawSprings();
>>>>>>> .r5
			drawNodes();
			//addEventListener(Event.ENTER_FRAME, update);
		}
		
		public function update(e:Event):void
		{
<<<<<<< .mine
			//drawSprings();
			makeNodes(rings);
			addVSprings();
			drawSprings();
			drawNodes();
=======
			for each (var n:webNode in nodes)
			{
				n.x=n.center.x;
				n.y=n.center.y;
			}
>>>>>>> .r5
		}
		
<<<<<<< .mine
		/////////////////////////////////////////////////
		//Recursively adds nodes to the list of nodes in the web in their correct position (starting in the center)
		//Also calles addHSprings while nodes are being added to put springs between them
		////////////////////////////////////////////////////
		public function makeNodes(n:Number):void
		{
			if (n >0)
			{
				trace("makeNodes called");
				/*if (!centerDrawn)
				{
					
				}*/
				
				for(var x:Number = 1; x<= sides; x++)
				{
					theta= x * (360/sides);
					makeNode(rads= theta * Math.PI / 180);
					addHSprings(x);
					v++;
				}
				radius += 60;
				makeNodes(n-1);
			}
			//parent.addChild(tempVertex);
		}
		
		////////////////////////////////////////////////////////////////////////////////////
		//goes through the nodes and makes the last ring of them a boolean of not movable
		//////////////////////////////////////////////////////////////////////////////////////
		public function setStaticNodes():void
		{
			for (var x:Number = nodes.length-1; x> nodes.length-sides; x--)
			{
				nodes[x].setStatic(false);
			}
		}
		
		/////////////////////////////////////////////////////////////////////
		//sets up event listeners for the movable nodes
		/////////////////////////////////////////////////////////////////////////////////
		public function setDrags():void
		{
			for each (var n:webNode in nodes)
			{
				if (n.canMove)
				{
					n.addEventListener(MouseEvent.MOUSE_DOWN, onpress);
				}
			}
		}
		
		public function onpress(e:MouseEvent):void
		{
			//trace("triggered");
			e.target.startDrag();
			e.target.removeEventListener(MouseEvent.MOUSE_DOWN, onpress);
			e.target.addEventListener(MouseEvent.MOUSE_UP, onrelease);
			trace(e.target.x+", "+e.target.y);
		}
		public function onrelease(e:MouseEvent):void
		{
			//trace("triggered");
			e.target.stopDrag();
			e.target.removeEventListener(MouseEvent.MOUSE_UP, onrelease);
			e.target.addEventListener(MouseEvent.MOUSE_DOWN, onpress);
		}
		//////////////////////////////////////////////////////
		//Makes a new spring depending on which iteration around the web we're on
		/////////////////////////////////////////////////////////////
		public function addHSprings(x:Number)
		{
			if (x>1 && x<=sides)
			{
				springs.push(new Spring(nodes[v], nodes[v-1]));
			}
			if (x==sides)
			{
				springs.push(new Spring(nodes[v], nodes[v-sides+1]));
			}
		}
		
		/////////////////////////////////////////////////////////////////
		//Adds a new webNode onto nodes with the rotation of rads from the previous node
		////////////////////////////////////////////////////////////////////////
		public function makeNode(rads:Number)
		{
			var tempNode:webNode = new webNode();
			tempNode.x = center.x+radius*Math.cos(rads);
			tempNode.y = center.y+radius*Math.sin(rads);
			nodes.push(tempNode);
			addChild(nodes[nodes.length-1]);
		}
		
		///////////////////////////////////////////////////////////////////
		//Goes through the list of vertices and adds springs between every 2 that should be connected.
		//////////////////////////////////////////////////////////////////////////////////////////
		public function addVSprings()
		{
			for(var x:Number = 1; x <=sides; x++)
			{
				springs.push(new Spring(nodes[0], nodes[x]));
			}
			for(var y:Number = sides+1; y < nodes.length; y++)
			{
				springs.push(new Spring(nodes[y], nodes[y-sides]));
			}
		}
		
		//////////////////////////////////////////////////
		//Goes through the list of Nodes and draws a circle at each one
		////////////////////////////////////////////////////
=======
		/////////////////////////////////////////////////
		//Recursively adds nodes to the list of nodes in the web in their correct position (starting in the center)
		//Also calles addHSprings while nodes are being added to put springs between them
		////////////////////////////////////////////////////
		public function makeNodes(n:Number):void
		{
			if (n >0)
			{
				trace("makeNodes called");
				var tempNode:webNode = new webNode(512, 384);
				nodes.push(tempNode);
				v++;
			
				for(var x:Number = 1; x<= sides; x++)
				{
					theta= x * (360/sides);
					makeNode(rads= theta * Math.PI / 180);
					addHSprings(x);
					v++;
				}
				radius += 60;
				makeNodes(n-1);
			}
			//parent.addChild(tempVertex);
		}
		
		////////////////////////////////////////////////////////////////////////////////////
		//goes through the nodes and makes the last ring of them a boolean of not movable
		//////////////////////////////////////////////////////////////////////////////////////
		public function setStaticNodes():void
		{
			for (var x:Number = nodes.length-1; x> nodes.length-sides-1; x--)
			{
				nodes[x].setStatic(false);
			}
		}
		
		/////////////////////////////////////////////////////////////////////
		//sets up event listeners for the movable nodes
		/////////////////////////////////////////////////////////////////////////////////
		public function setDrags():void
		{
			for each (var n:webNode in nodes)
			{
				if (n.canMove)
				{
					//n.addEventListener(MouseEvent.MOUSE_OVER, onpress);
					n.addEventListener(Event.ENTER_FRAME, onpress);
				}
			}
		}
		
		public function onpress(e:Event):void
		{
			//e.target.startDrag();
			//e.target.startDrag();
			
			
		}
		//////////////////////////////////////////////////////
		//Makes a new spring depending on which iteration around the web we're on
		/////////////////////////////////////////////////////////////
		public function addHSprings(x:Number)
		{
			if (x>1 && x<=sides)
			{
				springs.push(new Spring(nodes[v], nodes[v-1]));
			}
			if (x==sides)
			{
				springs.push(new Spring(nodes[v], nodes[v-sides+1]));
			}
		}
		
		/////////////////////////////////////////////////////////////////
		//Adds a new webNode onto nodes with the rotation of rads from the previous node
		////////////////////////////////////////////////////////////////////////
		public function makeNode(rads:Number)
		{
			nodes.push(new webNode(center.x+radius*Math.cos(rads),center.y+radius*Math.sin(rads)));
		}
		
		///////////////////////////////////////////////////////////////////
		//Goes through the list of vertices and adds springs between every 2 that should be connected.
		//////////////////////////////////////////////////////////////////////////////////////////
		public function addVSprings()
		{
			for(var x:Number = 1; x <=sides; x++)
			{
				springs.push(new Spring(nodes[0], nodes[x]));
			}
			for(var y:Number = sides+1; y < nodes.length; y++)
			{
				springs.push(new Spring(nodes[y], nodes[y-sides-1]));
			}
		}
		
		//////////////////////////////////////////////////
		//Goes through the list of Nodes and draws a circle at each one
		////////////////////////////////////////////////////
>>>>>>> .r5
		public function drawNodes():void
		{
<<<<<<< .mine
=======
			for each (var n:webNode in nodes)
			{
				n.render();
			}
		}
		
		public function drawSprings():void
		{
			graphics.beginFill(color); 
			graphics.lineStyle(thickness, color);
>>>>>>> .r5
			trace(graphics);
			for each (var s:Spring in springs)
			{
<<<<<<< .mine
				n.render();
=======
				graphics.lineStyle(thickness, 000000);
				graphics.moveTo(s.startNode.nodeX, s.startNode.nodeY);
				graphics.lineTo(s.endNode.nodeX, s.endNode.nodeY);
>>>>>>> .r5
			}
<<<<<<< .mine
		}
		
		public function drawSprings():void
		{
			graphics.clear();
			graphics.beginFill(color); 
			for each (var s:Spring in springs)
			{
				graphics.lineStyle(thickness, 000000);
				graphics.moveTo(s.startNode.x, s.startNode.y);
				graphics.lineTo(s.endNode.x, s.endNode.y);
			}
			
=======
			
>>>>>>> .r5
			graphics.endFill();
			
		}

	}
	
}
